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

Hello, 

I have a "data" that lists clinic names (one column of information), however there are subheadings. I would like to be able to separate the list by the subheadings. Example table below we have "Level 1 clinics" and "level II clinics". 

What I would like is to have dataset that lists all the clinics in level 1 as one column and the clinics under level II as another column. Not sure how to do this. Any assistance would be appreciate. I have the data step to create the table I have below. 

 

Table Have: 

level I clinics
Daisy clinic
Hewey clinic
Dewey OP clinic
Mickey House
Donald Clinic
level II clinics
Pluto Hosp
Goofy clinic
Horace House
Minnie clinic

 

Table want: 

level I clinicslevel II clinics
Daisy clinicPluto Hosp
Hewey clinicGoofy clinic
Dewey OP clinicHorace House
Mickey HouseMinnie clinic
Donald Clinic 

 

 

data have;
input Clinic_names $20.;
datalines;
level I clinics
Daisy clinic
Hewey clinic
Dewey OP clinic
Mickey House
Donald Clinic
level II clinics
Pluto Hosp
Goofy clinic
Horace House
Minnie clinic
;
run;

 

 

 

 

 

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

If you have different explicit levels of the variable you can replace the If =: with something like:

 

If clinic_names in ("first value text" "second value text" "third value" ) then do;

The IN operator is effectively: If variable = "first value" or variable="second value" or variable="third value" or variable = "4th value" . Or Is the value of the variable any of the values in the list. IN is case sensitive for character values. So if your data supplier isn't consistent about such you might need to do something like Upcase(variablename) in ("VALUE1" "OTHER VALUE") .

View solution in original post

3 REPLIES 3
ballardw
Super User

You will likely in the long run be better off having a separate variable that indicates whether the clinic is type 1 or 2 (or what ever)

consider:

data better;
   set have;
   length type $ 20.;
   retain type;
   if clinic_names =: 'level' then do;
      type=clinic_names;
      delete;
   end;
run;

which adds such a variable and removes the record with the level information.

If you haven't seen some of this before the RETAIN means keep the values across iterations of the data step.

The =:  means "begins with" so only need: If =: 'level' to identify the records where the header is the level.

Of course if you have a clinic whose name starts with level (and lower case here) complex logic would be needed, though not much.

 

Some brief examples of why I say that a separate variable can be more useful:

Grouping in reports

proc report data=better;
   columns type clinic_names;
   define type/ group;
run;

Counting just the clinic type

proc freq data=better;
   tables type;
run;

which output easily shows us that you have 55.6% roughly of type 1 clinics. You need to do a lot more coding to get such when there are two variables involved.

Similar if you go to graph something later and you want to see clinic patient numbers/diagnosis/pretty much anything then you use the Type variable in the Group role of the graphs in Sgplot or Sgpanel and the lines/symbols in the graph and its legend automatically are consistent and labeled. Two variables requires a tad more work to graph for the same affect.

Statistical analysis such as Ttest you use the Type variable in a Class statement to compare if measured variables such as blood pressure, BMI, or other numeric values have the same mean between the two.

sas_student1
Quartz | Level 8

@ballardw  this is great! This will work too, and thank you for the clarification on how the code works. 

However, I forgot to mention, in the example i gave the header started with "level" so you were able to use the =: to grab the value that started with the word level. But what if the subheadings are different. For example in this one one heading is "level I clinics" and the other heading is "non clinics". How would you suggest we handle that? 

can the statement  "if clinic_names =: 'level' then do" change to "if clinic_names in: ('level', 'non clinics') then do".

 

I think that would work. I tried it in the smaller datasets and I think it worked!

 

data have;
input Clinic_names $20.;
datalines;
level I clinics
Daisy clinic
Hewey clinic
Dewey OP clinic
Mickey House
Donald Clinic
Non clinics
Pluto Hosp
Goofy clinic
Horace House
Minnie clinic
;
run;
ballardw
Super User

If you have different explicit levels of the variable you can replace the If =: with something like:

 

If clinic_names in ("first value text" "second value text" "third value" ) then do;

The IN operator is effectively: If variable = "first value" or variable="second value" or variable="third value" or variable = "4th value" . Or Is the value of the variable any of the values in the list. IN is case sensitive for character values. So if your data supplier isn't consistent about such you might need to do something like Upcase(variablename) in ("VALUE1" "OTHER VALUE") .

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 3 replies
  • 1110 views
  • 0 likes
  • 2 in conversation