Hi all, i'm writing a script for data step.
the raw file is like this:
(dlm=,)
ID,visit_freq,sales1,sales2,sales3,sales4
A,3,2,3,
B,4,1,,2,4
C,2,1,2
output should be like this:
ID sales
A 2
A 3
A .
B 1
B .
B 2
B 4
C 1
C 2
i have used do while but the output is a bit different:
data sales1;
infile ' data path' dlm=',' missover;
input ID$ visit_freq @;
do until (visit_freq=0);
visit_freq=visit_freq-1;
input sales @;
output;
end;
drop visit;
run;
finally i got the output is different the expected (problem of observation B)... may anyone help on this????
Thanks in advance!!
You need the dsd option so that treats the adjacent delimiters (,,) as meaning a missing value. Otherwise it treats them the same way it would treat multiple spaces in normal list mode input.
data sales1;
infile 'data path' DSD dlm=',' TRUNCOVER;
input ID $ visit_freq @;
do until (visit_freq=0);
visit_freq=visit_freq-1;
input sales @;
output;
end;
drop visit_freq;
run;
Looks like your data tells you how many values to read. Use an iterative loop instead.
do i=1 to visit_freq ;
input sales @;
output ;
end;
Thanks Tom,
Actually it's similar to the do while/ until concept and the result would be like this?
ID sales
A 2
A 3
A .
B 1
B 2 **
B . **
B 4
C 1
C 2
i cant figure out why the rows with ** are swapped...
You need the dsd option so that treats the adjacent delimiters (,,) as meaning a missing value. Otherwise it treats them the same way it would treat multiple spaces in normal list mode input.
data sales1;
infile 'data path' DSD dlm=',' TRUNCOVER;
input ID $ visit_freq @;
do until (visit_freq=0);
visit_freq=visit_freq-1;
input sales @;
output;
end;
drop visit_freq;
run;
Thank you so much Tom!
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.