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.
Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.
Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.
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.