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

Hi guys, 

how can I write in "if, then" statement: if a condition = x then columnx = wordC1 wordC2 wordC3 wordC4 wordC5 wordC6 in a compact way? Let say:

>           if condition = 0 then columnx = wordC1-wordC6         

 

Edit:

suppose to have the following: 

>            ID

>            6

>            6

>            6

>            2

>            2

>            2

>            2

>            1

>            1           

 

Desired output: 

>            out

>            wordC1

>            wordC2

>            wordC3

>            wordP1

>            wordP2

>            wordP3

>            wordP4

>            wordF1

>            wordF2

 

>         if ID = 6 then wordC* (from 1 to 3 because there are 3 rows having 6 in ID column)

>         if ID = 3 then wordP* (from 1 to 4 because there are 4 rows having 2 in ID column)

>         if ID = 1 then wordF* (from 1 to 2 because there are 2 rows having 1 in ID column)

 

 

 

Thank you in advance

1 ACCEPTED SOLUTION

Accepted Solutions
Quentin
PROC Star

You can use a format (or similar) to store the mapping from ID= 1, 2,6 to C,P, F.  

 

Since your data are grouped by ID , you can use by-group processing to count records in each group.

 

So I think something like below should work, if I'm understanding what you want:

 

 

data have ;
  input id ;
  cards ;
6
6
6
2
2
2
2
1
1
;
run ;

proc format ;
  value type
   6='C'
   2='P'
   1='F'
  ;
run ;

data want ;
  set have ;
  by id notsorted ;

  if first.id then _count=0 ;
  _count++1 ;

  word=cats("word",put(id,type.),_count) ;

  drop _count ;
run ;

proc print data=want ;
run ;

Returns:

 

 

Obs    id     word

 1      6    wordC1
 2      6    wordC2
 3      6    wordC3
 4      2    wordP1
 5      2    wordP2
 6      2    wordP3
 7      2    wordP4
 8      1    wordF1
 9      1    wordF2

 

 

 

 

Check out the Boston Area SAS Users Group (BASUG) video archives: https://www.basug.org/videos.

View solution in original post

8 REPLIES 8
NewUsrStat
Quartz | Level 8
I edited the question with example to explain better the desired output
JosvanderVelden
SAS Super FREQ

Something like this?

data _null_;
   length column1 $256.;
   input var1 $ var2 $ var3 $ var4 $ var5 $ var6 $;
   if 1 then column1 = catx(" ", of var1-var6);
   put column1=;
   cards;
word1
word2
word3
word4
word5
word6
;
run;
ballardw
Super User

Where do the C  P and F come from? There is no obvious reason to insert those characters. Is there another rule you haven't mentioned.

 

I suggest you provide an actual example of your data set as I think there may be more than one or two things missing in your description. The data should be in the form of a working data step.

 

Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the </> icon or attached as text to show exactly what you have and that we can test code against.

NewUsrStat
Quartz | Level 8
Unfortunately they come from other calculations. I cannot change them. I have to use C, P and F.
Quentin
PROC Star

You can use a format (or similar) to store the mapping from ID= 1, 2,6 to C,P, F.  

 

Since your data are grouped by ID , you can use by-group processing to count records in each group.

 

So I think something like below should work, if I'm understanding what you want:

 

 

data have ;
  input id ;
  cards ;
6
6
6
2
2
2
2
1
1
;
run ;

proc format ;
  value type
   6='C'
   2='P'
   1='F'
  ;
run ;

data want ;
  set have ;
  by id notsorted ;

  if first.id then _count=0 ;
  _count++1 ;

  word=cats("word",put(id,type.),_count) ;

  drop _count ;
run ;

proc print data=want ;
run ;

Returns:

 

 

Obs    id     word

 1      6    wordC1
 2      6    wordC2
 3      6    wordC3
 4      2    wordP1
 5      2    wordP2
 6      2    wordP3
 7      2    wordP4
 8      1    wordF1
 9      1    wordF2

 

 

 

 

Check out the Boston Area SAS Users Group (BASUG) video archives: https://www.basug.org/videos.
NewUsrStat
Quartz | Level 8
Thank you very much! It is what I was looking for!
ballardw
Super User

@NewUsrStat wrote:
Unfortunately they come from other calculations. I cannot change them. I have to use C, P and F.

If they are calculated then you should share the calculation as that is needed as part of the solution of your question.

 

SAS INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

Register now!

From SAS Users blog
Want more? Visit our blog for more articles like these.
5 Steps to Your First Analytics Project Using SAS

For SAS newbies, this video is a great way to get started. James Harroun walks through the process using SAS Studio for SAS OnDemand for Academics, but the same steps apply to any analytics project.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 8 replies
  • 392 views
  • 3 likes
  • 5 in conversation