I have a number of repetitive lines of code which I want to be able to generate dynamically, the result should look something like this:
PROC SURVEYSELECT DATA=IBIS3_4 (where = (Exkl_UtgUtl_Flyg=3)) METHOD=SRS SAMPSIZE=&Exkl_UtgUtl_Flyg_kvot SEED=1234567 OUT=Exkl_UtgUtl_Flyg;
RUN;
PROC SURVEYSELECT DATA=IBIS3_4 (where = (Exkl_UtgUtl_Tag=3)) METHOD=SRS SAMPSIZE=&Exkl_UtgUtl_Tag_kvot SEED=1234567 OUT=Exkl_UtgUtl_Tag;
RUN;
I can do generate the SAS-code quite easily in R. I just define a vector with the relevant strings:
strings<-c("Exkl_UtgUtl_Flyg",
"Exkl_UtgUtl_Tag",
"Exkl_UtgUtl_Farja",
"Exkl_UtgUtl_Taxi",
"Exkl_UtgUtl_Hyrbil",
"Exkl_UtgUtl_Driv",
"Exkl_UtgUtl_Bo",
"Exkl_UtgUtl_Resta",
"Exkl_UtgUtl_Shop",
"Exkl_UtgUtl_Aktiv",
"Exkl_UtgUtl_Annat",
"Exkl_UtgSwe_Flyg",
"Exkl_UtgSwe_Tag",
"Exkl_UtgSwe_Farja",
"Exkl_UtgSwe_Taxi",
"Exkl_UtgSwe_Hyrbil",
"Exkl_UtgSwe_Driv",
"Exkl_UtgSwe_Bo",
"Exkl_UtgSwe_Resta",
"Exkl_UtgSwe_Shop",
"Exkl_UtgSwe_Aktiv",
"Exkl_UtgSwe_Annat")
And then I define a for-loop:
for (i in strings){print(paste0("* ",i," *
PROC SURVEYSELECT DATA=IBIS3_4 (where = (",i,"=3)) METHOD=SRS
SAMPSIZE=&",i,"_kvot SEED=1234567
OUT=",i,";
RUN;"))}
I have to copy-paste the output into MS Word and remove all quotes/row numbers/rowbreak-signs, but at least I don't have to program 20+ identical lines of code manually.
But is there a way of doing this entirily in SAS? Can I put a do-loop inside a put-statement or something similar, in order to generate the code that I need?
Try if this works. Without your data I cannot test it properly.
You don't need to create a new table for this. If those strings are part of a different table, just call that column in the last part.
data have;
input strings $40.;
datalines;
Exkl_UtgUtl_Tag
Exkl_UtgUtl_Farja
Exkl_UtgUtl_Taxi
Exkl_UtgUtl_Hyrbil
;
run;
Create a macro that will spam those proc surveyselect statements.
%macro Survey_select(strings);
PROC SURVEYSELECT DATA=IBIS3_4
(where = (&strings=3)) METHOD=SRS SAMPSIZE=%sysfunc(cats(&,&strings,_kvot)) SEED=1234567 OUT=&strings
;
RUN;
%mend Survey_select;
Call your macro with data step using call execute. If those strings are not in a table of their own, you can easily call them from some other table in this step.
data _null_;
set have;
call execute(cats('%Survey_select(',strings,')'));
run;
Related
I can”t understood, why I can print the value of rows, but not populate this to a tkinter entry.
My code:
cursor.execute(‘SELECT * FROM contacts;’)
print(‘row in table contacts:’,len(cursor.fetchall())) # prints 104
self.no_count.set(len(cursor.fetchall())) # populate 0
Any hint?
You should store the fetched data inside a variable and then access it through the variable. This is because a cursor is like a python generator, and once you use cursor.fetchall() the results will no longer contain the result again. So go for something like:
cursor.execute('SELECT * FROM contacts;')
data = cursor.fetchall() # Store in variable
print(f'row in table contacts: {len(data)}') # Used f strings instead of comma(can be ignored)
self.no_count.set(len(data))
Or you could also go for the inefficient way of repeating your query each time, like:
cursor.execute(‘SELECT * FROM contacts;’)
print(f‘row in table contacts: {len(cursor.fetchall())}')
cursor.execute(‘SELECT * FROM contacts;’) # Repeat the query
self.no_count.set(len(cursor.fetchall())) # Fetch again
I have the following table in Lua:
local a = {orszag = {"Ausztria", "Albánia", "Azerbajdzsán"}, varos = {"Ankara", "Amszterdam", "Antwerpen"}, fiu = {"Arnold", "Andor", "Albert"}, lany = {"Anna", "Anasztázia", "Amanda"}}
I would like to do the following:
for i in a["orszag"] do etc. (for example compare all the words in the value to the user input)
But when I do so I get the following: attempt to call a table value.
So I know, it works in python for example, but is it possible somehow to do this in Lua as well?
Use
for k,v in pairs(a["orszag"]) do
I'm new to Powershell and I'm putting together a script that will populate all variables from data stored in a Excel file. Basically to create numerous VMs.
This works fine apart from where i have a variable with multiple name/value pairs which powershell needs to be a hashtable.
As each VM will need multiple tags applying, i have a column in excel called Tags.
The data in the field would look something like: "Top = Red `n Bottom = Blue".
I'm struggling to use ConvertFrom-StringData to create the hashtable however for these tags.
If i run:
ConvertFrom-StringData $exceldata.Tags
I end up with something like:
Name Value
---- -----
Top Red `n bottom = blue
I need help please with formatting the excel field correctly so ConvertFrom-StringData properly creates the hashtable. Or a better way of achieving this.
Thanks.
Sorted it, formatted the excel field as: Dept=it;env=prod;owner=Me
Then ran the following commands. No ConvertFrom-StringData required.
$artifacts = Import-Excel -Path "C:\temp\Artifacts.xlsx" -WorkSheetname "VM"
foreach ($artifact in $artifacts) {
$inputkeyvalues = $artifact.Tags
# Create hashtable
$tags = #{}
# Split input string into pairs
$inputkeyvalues.Split(';') |ForEach-Object {
# Split each pair into key and value
$key,$value = $_.Split('=')
# Populate $tags
$tags[$key] = $value
}
}
My code is written in a file "plot.m".
If I put the following code in "plot.m", when I call plot("20%"), the Octave GUI will keep opening a new window with a new figure indefinitely.
function X = plot(folderName)
X = 0;
data = ([folderName, "\\summary.txt"]);
NUM_SURVIVED = data(1);
NUM_DATA = size(data)(1)-1;
FINAL_WEALTH = data(2 : NUM_DATA);
%plot FINAL_WEALTH
figure;
plot(1:numel(FINAL_WEALTH), FINAL_WEALTH, '-b', 'LineWidth', 2);
xlabel('x');
ylabel('FINAL_WEALTH');
end
However, if I put the following code in "plot.m" and run it, the program works as intended and will plot data from "summary.txt".
data = ("20%\\summary.txt");
NUM_SURVIVED = data(1);
NUM_DATA = size(data)(1)-1;
FINAL_WEALTH = data(2 : NUM_DATA);
%plot FINAL_WEALTH
figure;
plot(1:numel(FINAL_WEALTH), FINAL_WEALTH, '-b', 'LineWidth', 2);
xlabel('x');
ylabel('FINAL_WEALTH');
Any idea what I am doing wrong in the first section of code? I would like to write it as a function so that I can call it multiple times for different folder names.
When you call plot from the function plot, you get endless recursion. Rename your function and its file.
Just adding to Michael's answer, if you really wanted to name your function as "plot" and override the built-in plot function, but still wanted to be able to call the built-in plot function inside it, this is actually possible to do by using the builtin function to call the built-in version of plot. Your code would then look like this:
function X = plot (folderName)
% same code as before
figure;
builtin ("plot", 1:numel(FINAL_WEALTH), FINAL_WEALTH, '-b', 'LineWidth', 2);
xlabel ('x');
ylabel ('FINAL_WEALTH');
end
Obviously, whether it's a good idea to overload such a core function in the first place is an entirely different discussion topic. (Hint: don't!)
I have an autoexec file that encrypts my password when I'm connecting to different servers....the code looks as follows:
%global wspwd ewspwd hpwd ehpwd ;
/* Enter WORKSTATION Password Below */
filename ewspwdfl "/home/&sysuserid./ewspwd.txt" ;
proc pwencode in=’XXXXXXXX’ out=ewspwdfl ; run ;
data _null_ ;
infile ewspwdfl obs=1 length=l ;
input # ;
input #1 line1 $varying1024. l ;
call symput('ewspwd',cats(substr(line1,1,l))) ;
call symput('wspwd',cats(‘XXXXXXXX’)) ;
run ;
My question is: why is
input # ;
included and why
input #1 line1 $varying1024. l ;
doesn't suffice.
Whenever I have created datasets with SAS I have never had to include "input #;" in my statement. I just simply write something along the lines of:
input #1 firstname $ #15 lastname $ #30 date mmddyy6.;
You don't need it for that data step. You could simplify it to.
data _null_ ;
infile ewspwdfl obs=1 TRUNCOVER ;
input line1 $CHAR1024. ;
call symputX('ewspwd',line1);
call symputX('wspwd',‘XXXXXXXX’) ;
run ;
Using input # is a good way to create a program where you want to read different lines using different input statements. You could test the content of the _infile_ variable and execute different parts of the data step based on what is read.
It is also useful when using the EOV= option on the INFILE statement to detect when you are starting to read from a new file, since it is not set until you begin reading the new file. So the INPUT # gets SAS to begin reading so that the EOV variable is set, but keeps the line waiting for your real INPUT statement to read later.
The #1 is useful if you want to read the same columns over again into different variables. For example you might want to read the first few characters as a string and then test them and based on what you find re-read as a number or a date.