Dear all,
I have a macro variable with a comma separated list of numbers: %let a = 1,2,3,4,5. I'd like to create a sas data set now with one var called id that has 5 rows of which one row contains one number from the comma separated list. Just like in:
data myoutput;
input id 1;
datalines;
1
2
3
4
5
;
run;
I though I could mybe do this:
data myoutput;
input id 1;
datalines;
&linebreaklist;
run;
And create the Makrovariable &linebreaklist like this:
%let linebreaklist = %sysfunc(tranwrd(%str(1,2,3,4,5),%str(,),\n));
Of course with the above statement I get
1\n2\n3\n4\n5
But I'd like to have
1
2
3
4
5
Does anybody know how I'd have to specify the linebreak in the tranwrd() function?
Best wishes
Eva
%let a=1,2,3,4,5;
filename list temp;
data _null_;
file list;
put "&a";
run;
data want ;
infile list dsd ;
input id @@;
run;
I have used scan function for cases like this:
%let a = 1,2,3,4,5;
data myoutput(drop=count);
retain count 0;
b='.';
do while (b ne '');
count+1;
b=scan("&a",count,',');
if b ne '' then output;
end ;
run;
Asko
Dear Asko,
yes, that's what I use now. I thought maybe there's a solution without a loop.
You can't do much computer programming withOUT loops. This one has a loop too. Can you find it?
😉 Of course I know where the loop is.
I just wanted to have something easier to look at without do while.
I found a hint about @@ in some other forum as well. But haven't found much about what it does. Do you know where I find that in the documentation or could you explain it?
INPUT statement.
I think you could do it if you wanted a text file output (just replace the comma with the ascii crlf (which i think is '0a0d'x). I don't think sas datasets work like that as you need to output a new row instead of just replacing the text.
if u just want result then try it,, if macro variable is nt necessary for u..
data test;
x='1,2,3,4,5';
do i= 0 to length(x);
y=substr(x,i+1,1);
i=i+1;
output;
end;
drop i x;
run;
proc print;
run;
Mmh, ok I see. So I'll have a look at @@ in the input statement and then decide whether I use this or the loop.
Thanx a lot for all your helpful answers - I learned a lot!
Another factor may influence your decision here ... where do you plan to put this code? The CARDS statement is not permitted inside a macro definition.
DN, I found that this small simplification is permissible:
_infile_="&a";
%let a=1,2,3,4,5;
filename list temp;
data _null_;
file list;
put "&a";
run;
data want ;
infile list dsd ;
input id @@;
run;
data test;
input month $ sale;
datalines;
jan 2000
feb 1400
mar 2300
apr 3400
may 2700
jun 3200
july 800
aug 2900
sep 1700
oct 2100
nov 2600
dec 3200
;
run;
i m looking to convert month in num as 1 2 3 ,
for that i m tring it.
data test1;
set test;
mon_num=input(month,monname3.);
run;
proc print; run;
but its showing error. i need to short data by month. can we do it in short or sud i use if condition on each month????
data have;
input month $ sale;
datalines;
jan 2000
feb 1400
mar 2300
apr 3400
may 2700
jun 3200
july 800
aug 2900
sep 1700
oct 2100
nov 2600
dec 3200
;
run;
data want;
format
Month_Num 8.;
set have;
month_num=month(input(month||' 01, 2000',anydtdte20.0));
run;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.