sas-macro

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

拜拜、爱过 提交于 2019-12-22 12:30:48
问题 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

When to use IF or %IF in SAS

时光毁灭记忆、已成空白 提交于 2019-12-22 10:07:37
问题 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;

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

半世苍凉 提交于 2019-12-22 09:39:02
问题 %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

SAS: export data to multiple csv file by year

橙三吉。 提交于 2019-12-21 18:11:50
问题 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! 回答1: 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

Parse JSON object in SAS macro

只谈情不闲聊 提交于 2019-12-17 16:53:47
问题 Here is the input JSON file. It have to parse in SAS dataset. "results": [ { "acct_nbr": 1234, "firstName": "John", "lastName": "Smith", "age": 25, "address": { "streetAddress": "21 2nd Street", "city": "New York", "state": "NY", "postalCode": "10021" } } , { "acct_nbr": 3456, "firstName": "Sam", "lastName": "Jones", "age": 32, "address": { "streetAddress": "25 2nd Street", "city": "New Jersy", "state": "NJ", "postalCode": "10081" } } ] And I want the output for only Address field in SAS

SAS Macro quoting issues

主宰稳场 提交于 2019-12-14 02:35:24
问题 I am trying to perform operations on binary data saved into a macro variable. The datastep below successfully saves the data into the macro variable without any issues: data _null_; infile datalines truncover ; attrib x length=$300 informat=$300. format=$300.; input x $300.; put x=; call symput ('str',cats(x)); datalines4; ‰PNG > IHDR ) ) ëŠZ sRGB ®Î=é gAMA ±^üa pHYs ;à ;ÃÇo¨d ZIDAT8OåŒ[ À½ÿ¥Ó¼”Ö5Dˆ_v@aw|+¸AnŠ‡;6<ÞóRÆÒÈeFõU/'“#f™Ù÷&É|&t"<ß}4¯à6†Ë-Œ_È(%<É'™èNß%)˜Î{- IEND®B`‚ ;;;; run; When I

SAS Loop over a list of variables inside a macro (read one each time)

隐身守侯 提交于 2019-12-13 18:42:00
问题 I would need to do a loop over a list of variables inside a macro. The list is created in the following way (I have started the name of the variables that I want with MO, nu or KA): proc sql noprint; select name into :varsi separated by ' ' from dictionary.columns where libname eq 'LABIMP' and memname eq 'MUESTRA1' and (NAME LIKE 'MO_%' OR NAME LIKE 'nu_%' or name like 'KA_%'); quit; Then, I need to run a macro for each one... this macro is inside the following data step: data labimp.muestra1

Combine different SQL into one table using SAS

会有一股神秘感。 提交于 2019-12-13 08:39:10
问题 I would like to know how to combine different SQL queries into one table. The format should be customer_no|TOTAL_DIFF_LASTPAYMENT_OPENED_dt|utilization trend| count_enquiry_recency_365|ratio_currbalance_creditlimit I have two files with data (see data sample). I want to create a new table that contains the output of these PROC's. Data sample: https://www.dropbox.com/sh/k7qz6m8w0yqenn7/AAAgYcVhHxCRNhiX5ZvK0q5Aa?dl=0 /* TOTAL_DIFF_LASTPAYMENT_OPENED_dt */ proc sql; select customer_no, avg(TOTAL

quote array element inside macro do loop

蓝咒 提交于 2019-12-13 05:27:14
问题 I can call the macro VIO by %VIO(Dow=Fri,Hour=8) without error. Now I want to nest the call macro inside a do loop. Here's what I have tried but no luck. %macro trythis; data _null_; array ay[3] $3 ('Fri' 'Sat' 'Sun'); %do i = 1 %to dim(ay); %do j = 1 %to 24; %VIO(ay[&i],&j); %end; %end; run; %mend; %macro VIO(Dow,Hour); data Want&Dow.&Hour; set have(where=(hour="&Hour")); format dow $3.; dow = "&Dow"; run; %mend; It seems ay[&i] will resolve to ay[1] not Fri . 回答1: You can't exchange data

Why does my code inside my macro is not taken into account?

空扰寡人 提交于 2019-12-13 05:19:56
问题 I'm trying to code a macro on SAS that is able to create 50 different sample of 1500 people. But as soon as I enter %macro , all the following code is not taken into account properly (PROC SURVEYSELECT, DATA, RUN... Do not have any color anymore). You'll find below my code, can you please have a look? %macro loop(50); %do i=1 %to 50; PROC SURVEYSELECT DATA=WORK.TOP_1() METHOD=SRS OUT= WORK.ALEA_1 N=1500; RUN; %end; %mend; %loop(50); 回答1: This is just the usual behaviour of the Enhanced Editor