DATA Step, Macro, Functions and more

How to reshape comma-delimited values in SAS dataset?

Accepted Solution Solved
Reply
Contributor
Posts: 54
Accepted Solution

How to reshape comma-delimited values in SAS dataset?

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?


Accepted Solutions
Solution
‎01-26-2015 03:17 AM
Super User
Posts: 9,687

Re: How to reshape comma-delimited values in SAS 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;

View solution in original post


All Replies
Trusted Advisor
Posts: 1,131

Re: How to reshape comma-delimited values in SAS dataset?

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

Thanks,
Jag
Solution
‎01-26-2015 03:17 AM
Super User
Posts: 9,687

Re: How to reshape comma-delimited values in SAS 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;

Contributor
Posts: 54

Re: How to reshape comma-delimited values in SAS dataset?

thank you xia keshan!

Super User
Posts: 6,963

Re: How to reshape comma-delimited values in SAS dataset?

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 248 views
  • 2 likes
  • 4 in conversation