BookmarkSubscribeRSS Feed
sasprogramming
Quartz | Level 8

I want to create a dummy variable column for every code. This normally wouldn't be hard but because I am looking for substrings in a string it has stumped me.

 

Current data set:

IDCode
1280A001B007
2125A001B007
2170B002
2413A001C023
2440C023

 

Desired data set:

IDA001B002B007C023
12801010
21251010
21700100
24131001
24400001

 

Thanks for any help.

6 REPLIES 6
Kurt_Bremser
Super User
data long;
set have;
length name $4;
value = 1;
do i = 1 to length(code) by 4;
  name = substr(code,i,4);
  output;
end;
keep id name value;
run;

proc transpose 
  data=long
  out=want
;
by id;
var value;
id name;
run;

/* alternatively */
proc report data=long;
column id name;
define id / group;
define name / "" across;
run;

Untested, posted from my tablet.

sasprogramming
Quartz | Level 8

This doesn't work unofortunately

Kurt_Bremser
Super User

"Doesn't work" on its own is not helpful. Please invest some effort into your answers.

 

If you mean that you want zeroes instead of missing values, you need a standardization step after the transpose:

proc transpose 
  data=long
  out=pre_want;
;
by id;
var value;
id name;
run;

proc stdize
  data=pre_want
  out=want (drop=_name_)
  missing=0
  reponly
;
var _numeric_;
run;

proc print data=want noobs;
run;

Result:

ID	A001	B007	B002	C023
1280	1	1	0	0
2125	1	1	0	0
2170	0	0	1	0
2413	1	0	0	1
2440	0	0	0	1

or, for the PROC REPORT, add

options missing=0;

(same result)

sasprogramming
Quartz | Level 8
The code is working almost fine, what I end up with is the following:

ID name value
1280 A001 1
1280 B007 1
2125 A001 1
2170 B007 1
2413 A001 1
2413 C023 1
2440 C023 1
andreas_lds
Jade | Level 19

@sasprogramming wrote:
The code is working almost fine, what I end up with is the following:

ID name value
1280 A001 1
1280 B007 1
2125 A001 1
2170 B007 1
2413 A001 1
2413 C023 1
2440 C023 1

Seems that something is wrong in the code you actually submitted. Please share the code using "Insert SAS Code" button.

Kurt_Bremser
Super User

@sasprogramming wrote:
The code is working almost fine, what I end up with is the following:

ID name value
1280 A001 1
1280 B007 1
2125 A001 1
2170 B007 1
2413 A001 1
2413 C023 1
2440 C023 1

That's the "long" dataset; from this you run TRANSPOSE and STDIZE, or PROC REPORT with OPTIONS MISSING=0.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 517 views
  • 0 likes
  • 3 in conversation