Hi,
I have a SAS dataset as follows:
data source;
input id cols $10.;
cards;
1 gender,age
2 address
;
run;
Now, I would like to transpose those values in COLS column which is currently separated by a comma. The target dataset is as follows:
data target;
input id cols $;
cards;
1 gender
1 age
2 address
;
run;
How will I be able to achieve the target dataset?
data source;
input id cols $10.;
cards;
1 gender,age
2 address
;
run;
data source2;
set source;
retain id;
do i = 1 to countw(cols,',');
new_cols=scan(cols,i,',');
if new_cols ne '';
output;
end;
drop i;
run;
Please try
data source;
input id cols $10.;
cards;
1 gender,age
2 address
;
run;
data source2;
set source;
retain id;
do i = 1 to 2;
new_cols=scan(cols,i,',');
if new_cols ne '';
output;
end;
drop i;
run;
Thanks,
Jag
data source;
input id cols $10.;
cards;
1 gender,age
2 address
;
run;
data source2;
set source;
retain id;
do i = 1 to countw(cols,',');
new_cols=scan(cols,i,',');
if new_cols ne '';
output;
end;
drop i;
run;
thank you xia keshan!
Interesting. From the SAS documentation for the subsetting if:
If the expression is false (its value is 0 or missing), no further statements are processed for that observation or record, the current observation is not written to the data set, and the remaining program statements in the DATA step are not executed. SAS immediately returns to the beginning of the DATA step because the subsetting IF statement does not require additional statements to stop processing observations.
(emphasis by me)
This would imply that a value for cols of "Age,,Gender" would lead to "Gender" being dropped, but that does actually not happen. SAS only exits the do loop.
Learning never stops.
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.