BookmarkSubscribeRSS Feed
RandyStan
Fluorite | Level 6

Dear All:

   My data is as follows:

Name  

A

AA
AAA

AB

ABB

ABC

ACC

 

And so forth

 

I want to contruct Variables such that the data set now looks like

 

Name       Var3      Var5

A                 1           1

AA               1           1
AAA             1           1

AB                             1

ABB                           1

ABC

ACC

 

It is possible to construct this data set with multiple steps.  But is there a shorter way.

   Thanks

    Randy

5 REPLIES 5
ballardw
Super User

What are the rules for assigning those values to the other variables?

 

Sometimes if you have something like "I have to look at many records with this value for ID and then assign the (max, min, mean something else from the whole set) you may have to summarize the data and then remerge.

But without some idea why a record gets var3 set to 1 and why set to missing anything would be a shot in the dark.

Shmuel
Garnet | Level 18

Are var3 var5 new varaibles to be added ?

What is the logic rules to create those variables? 

Rick_SAS
SAS Super FREQ

Please show us how you do it with multiple steps so that we can understand your logic.

RandyStan
Fluorite | Level 6

data group3 ; set names;

format Gr3 1.

gr3 = 1 ;

run;

 

data group3 ; set group3;

if _N_ gt 3 then delete;

run;

 

data group5 ; set names;

format Gr5 1.

gr5 = 1 ;

run;

 

data group5 ; set group5;

if _N_ gt 5 then delete;

run;

 

data want;

merge names group3 group5;

by name;

run;

 

 

ballardw
Super User

You mean like:

 

data want;
   set names;
   if _n_ le 3 then Gr3=1;
   if _n_ le 5 then Gr5=1;
run;
 

Note your example code was missing ;

 

The second data step to subset is redundant

data group3 ; 
   set names;
   format Gr3 1.   ;
   gr3 = 1 ;
   if _N_ gt 3 then delete;

run;

The forum message windows will sometimes reformat text. It is best to post code and log info into a code box opened with the {i} icon in the menu row at the top of the window.

 

sas-innovate-2024.png

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.

 

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
  • 5 replies
  • 847 views
  • 0 likes
  • 4 in conversation