BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
AwesomeSAS
Calcite | Level 5

Consider my data as below,

IDOBSQ2A1Q2B1Q2C1
100911Upper TrunkAnteriorLeft
100912Upper TrunkPosteriorLeft

 

What i want is:

ResultFLAG VARIABLE 
Upper Trunk1
Anterior1
Left1
Upper Trunk2
Posterior2
Left2

I am able to derive the Result column but accordingly for 1st OBS, I need a flag variable assigned. Please help here, I am a novice programmer.

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Please try to provide data in the form of data step code as below. It will make responses much more usable and avoid things like variable type questions.

For what you show:

data have;
   infile datalines dlm=',';
   informat id $5. obs 5. Q2A1	Q2B1	Q2C1 $15.;
   input ID	OBS	Q2A1	Q2B1	Q2C1;
datalines;
10091,1,Upper Trunk,Anterior,Left
10091,2,Upper Trunk,Posterior,Left
;

proc transpose data=have 
   out=want (rename=(obs=flagvariable) drop=_name_)
;
  by obs;
  var Q2A1	Q2B1	Q2C1;
run;

However the Proc transpose assumes the data is sorted by OBS, that there are not multiple duplicate values of any of the Q variables. I have interpreted "accordingly for 1st OBS" to mean "use the value of the OBS variable as the flag". If that is not what is intended then you need to provide more rules.

 

Note that the code is posted into a text box opened using the </> icon at the top of the message window. The main message windows on this forum will reformat text pasted into the window and may result in code that does not run properly when copied out and pasted into the SAS editor.

 

 

View solution in original post

1 REPLY 1
ballardw
Super User

Please try to provide data in the form of data step code as below. It will make responses much more usable and avoid things like variable type questions.

For what you show:

data have;
   infile datalines dlm=',';
   informat id $5. obs 5. Q2A1	Q2B1	Q2C1 $15.;
   input ID	OBS	Q2A1	Q2B1	Q2C1;
datalines;
10091,1,Upper Trunk,Anterior,Left
10091,2,Upper Trunk,Posterior,Left
;

proc transpose data=have 
   out=want (rename=(obs=flagvariable) drop=_name_)
;
  by obs;
  var Q2A1	Q2B1	Q2C1;
run;

However the Proc transpose assumes the data is sorted by OBS, that there are not multiple duplicate values of any of the Q variables. I have interpreted "accordingly for 1st OBS" to mean "use the value of the OBS variable as the flag". If that is not what is intended then you need to provide more rules.

 

Note that the code is posted into a text box opened using the </> icon at the top of the message window. The main message windows on this forum will reformat text pasted into the window and may result in code that does not run properly when copied out and pasted into the SAS editor.

 

 

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 1 reply
  • 376 views
  • 0 likes
  • 2 in conversation