BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Eva
Quartz | Level 8 Eva
Quartz | Level 8

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

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

%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;

View solution in original post

12 REPLIES 12
AskoLötjönen
Quartz | Level 8


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

Eva
Quartz | Level 8 Eva
Quartz | Level 8

Dear Asko,

yes, that's what I use now. I thought maybe there's a solution without a loop.

data_null__
Jade | Level 19

You can't do much computer programming withOUT loops.  This one has a loop too. Can you find it?

%let a = 1,2,3,4,5;

data a;
   retain a "&a";
  
infile cards eof=eof dsd;
  
if _n_ eq 1 then do;
     
input @@;
      _infile_ = a;
     
end;
  
input n @@;
   return;
eof:
stop;
  
cards;
!necessary evil!
;;;;;
   run;
Eva
Quartz | Level 8 Eva
Quartz | Level 8

😉 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?

DBailey
Lapis Lazuli | Level 10

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.

Aman4SAS
Obsidian | Level 7

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;

Eva
Quartz | Level 8 Eva
Quartz | Level 8

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!

Astounding
PROC Star

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";

Tom
Super User Tom
Super User

%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;

Aman4SAS
Obsidian | Level 7

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????

DBailey
Lapis Lazuli | Level 10

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: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 12 replies
  • 5575 views
  • 2 likes
  • 7 in conversation