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

Hello,

 

I am working with a nationally representative data (three waves). The stratum (ststr) variable is not available for one wave, which I need to create. Here is what I got from the person who responded to my inquiry about the missing variable:

 

"STSTR is a five digit number that combines the values for state_flips (first two characters), geostrs_samp (third and fourth character), and denstrs_samp (final character). You need to covert state_flips, geostrs_samp, denstrs_samp into character variables first. Please add “0” to single digit number if state_flips or geostrs_samp only have one digit. For example, state_flips = 1, then add 0 at beginning. It should be 01. After combining three values together, you could either convert it back a numeric variable or keep it as a character variable."

 

I am not sure how to do these steps. I found the code below that allows me to break up a variable into two and extract certain characters into each of the new variables. But I could not find anything that will help me create the ststr variable. If you can also help me with adding "0" to the one-digit variables, I would greatly appreciate it. Please help.

 

data pieces_parts;
input id $ 1-9;
length state $ 2;
state = substr(id,1,2);
num = input(substr(id,7,3),3.);
datalines;
NYXXXX123
NJ1234567
;
proc print data= pieces_parts noobs;
title "Listing of Data Set PIECES_PARTS";
run;

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

Evidently, these variables are all numeric:

 

  • state_flips
  • geostr_samp
  • denstr_samp

 

To create a character string out of all these, you would use (in a DATA step):

 

ststr = put(state_flips, z2.) || put(geostr_samp, z2.) || put(denstr_samp, 1.);

View solution in original post

3 REPLIES 3
Astounding
PROC Star

Evidently, these variables are all numeric:

 

  • state_flips
  • geostr_samp
  • denstr_samp

 

To create a character string out of all these, you would use (in a DATA step):

 

ststr = put(state_flips, z2.) || put(geostr_samp, z2.) || put(denstr_samp, 1.);
Reeza
Super User
PUT() converts numbers to characters.
Zd formats displays your numeric data with leading zero.

CATT() will concatenate the variables without any extraneous spaces or padding, because you can often get them added in when using other methods.
ballardw
Super User

Are you sure that the statement was "state flips" not "state fips"? FIPS is a standard coding system, Federal Information Processin Standards.

 

SAS even has functions that use the FIPS coding such as Pipstate, Fipname, Fipnamel, Stfips, that convert Fips codes to state names or state abbreviations to Fip values.

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
  • 3 replies
  • 549 views
  • 2 likes
  • 4 in conversation