sas-macro

Random sampling without replacement in longitudinal data

久未见 提交于 2019-12-06 13:21:43
My data is longitudinal. VISIT ID VAR1 1 001 ... 1 002 ... 1 003 ... 1 004 ... ... 2 001 ... 2 002 ... 2 003 ... 2 004 ... Our end goal is picking out 10% each visit to run a test. I tried to use proc SURVEYSELECT to do SRS without replacement and using "VISIT" as strata. But the final sample would have duplicated IDs. For example, ID=001 might be selected both in VISIT=1 and VISIT=2. Is there any way to do that using SURVEYSELECT or other procedure (R is also fine)? Thanks a lot. This is possible with some fairly creative data step programming. The code below uses a greedy approach, sampling

A macro function to produce a macro variable from a data variable

只谈情不闲聊 提交于 2019-12-06 07:08:25
data sample; input x $; datalines; one two three ; %macro variable_to_macvar(variable=, dataset=); proc sql noprint; select &variable into : outlist separated by ' ' from &dataset; quit; &outlist %mend variable_to_macvar; %put %variable_to_macvar(variable=x, dataset=sample); Expected output: one two three . Instead I get an error. Why? Is this fixable? I've successfully created other macros of a very similar form, where the function "returns" a value using the &macrovariable at the end of the macro without a semicolon. For example, here is a similar type of function that works: %macro zcat

Why does the %str in `%str(%inner_macro())` not work?

余生颓废 提交于 2019-12-06 03:54:08
%str should pass a string as one parameter to a sas macro, even if it contains commas, but that apparently does not work if the argument of %str is in it self the result of a macro? I get ERROR: More positional parameters found than defined. Example This is what the error means 15 %macro outer_macro(left, right); 16 %put NOTE: outer_macro: left is &left; 17 %put NOTE: outer_macro: right is &right; 18 %mend; 19 %outer_macro(left, right); NOTE: outer_macro: left is left NOTE: outer_macro: right is right 20 %outer_macro(left, midle, right); ERROR: More positional parameters found than defined.

When to use IF or %IF in SAS

佐手、 提交于 2019-12-06 03:53:55
I am new to SAS and having a hard time figuring out when should the simple If-Then-else and when should %IF-%THEN-%ELSE should be used. As an example code below: %let inFile = %scan(&sysparm, 1, " "); %macro read_data(infile); data want; infile "&infile" LRECL=1000; retain fdate; if _n_ = 1 then do; input Agency $ Status $ Num $ fdate sdate; end; else do; %if fdate < 20130428 %then input @1 poolno $6. @7 factor 9.8 @; %else input @1 rectype $1 @3 poolno $6. @9 factor 9.8 @; @18 pfactor 9.8; output; end; drop Agency Status Num sdate; run; proc print data=want; run; %mend read_data; %read_data(

submit SAS code or macro from Toolbar

孤街浪徒 提交于 2019-12-06 03:06:59
问题 Is it possible to allocate a SAS script or macro to a Toolbar button in Base SAS? ie can you 'dm' a macro or sas script? 回答1: Certainly. Here is one way: Go to Tools->Customize. Select the Customize Tab Create a new blank button by clicking the "Add Tool" (left most button, right above the word "command" Select an icon for the new button using the "change icon" button (otherwise it will be blank and won't show up in the toolbar) To have the button submit a compiled macro, type this in the

Parsing header line separately in SAS

夙愿已清 提交于 2019-12-05 20:20:17
I have input file, in which the first line has header information. (Data values which are tab-separated). Amongst these values, there is an integer value that specifies how the rest of the file should be parsed. If this value is less than a threshold value, then the file should be parsed in one way, otherwise if the value is greater that the threshold value, then the data should be parsed in a different way. %let isNew = Undefined; data header; infile "&infile" OBS=1; INPUT Agency $ Status $ Num $ fdate sdate; if fdate < 20130428 then %let isNew = false; else %let isNew = true; run; data

Finding all possible paths in a dataset using sas

ε祈祈猫儿з 提交于 2019-12-04 21:21:42
I want to compare two columns in the dataset shown below Pid cid 1 2 2 3 2 5 3 6 4 8 8 9 9 4 Then produce a result like below 1 2 3 6 1 2 5 2 3 6 2 5 3 6 4 8 9 4 8 9 4 9 4 First we print the first two values 1 and 2, search for 2 in first column, if its present print its corresponding value from column 2, which is 3. Search for 3 in column 1, if present print the corresponding value from column 2 which is 6 How can this be done using SAS? The links comprise a directed graph and need recursion to traverse the paths. In data step, the multiple children of a parent can be stored in a Hash of

SAS: export data to multiple csv file by year

帅比萌擦擦* 提交于 2019-12-04 10:54:35
I have a dataset in SAS, which contains 20 years of data. I want to export to csv file for each year. Is there any easy way to do it? Here is what I'm doing for one year now (which is naive): proc export data=ds (where=(year=2011)) outfile='ds2011.csv' DBMS=CSV replace; run; Thanks a lot! Non-macro option: You can use the file statement in a data step to write out various types of text or delimited files. Using the filevar option allows you to create one file for each value of a variable as is done below. Replace yourdata with your dataset and write_location with where you want the file to go.

submit SAS code or macro from Toolbar

…衆ロ難τιáo~ 提交于 2019-12-04 08:22:04
Is it possible to allocate a SAS script or macro to a Toolbar button in Base SAS? ie can you 'dm' a macro or sas script? Certainly. Here is one way: Go to Tools->Customize. Select the Customize Tab Create a new blank button by clicking the "Add Tool" (left most button, right above the word "command" Select an icon for the new button using the "change icon" button (otherwise it will be blank and won't show up in the toolbar) To have the button submit a compiled macro, type this in the command field (substituting your macro name of course): %nameofmacro;run; To have the button submit an external

SAS Macro, passing value as string to where clause

て烟熏妆下的殇ゞ 提交于 2019-12-04 04:31:39
问题 I have a SAS macro below that is not working--- this snippet returns no values because the where statement doesn't work. Anyone have any ideas? I tried adding %str but that didn't work either. %macro refreshments(beverage_type=); proc sql; select * where drink_type = '&beverage_type.' ; quit; %mend %refreshments(Sprite); Thanks. 回答1: Macro variables will not resolve in single quotes. You are also missing the FROM clause, and the macro parameter was being provided as positional (instead of